package com.jd.org.service.impl;


import com.jd.basic.service.impl.BaseServiceImpl;
import com.jd.basic.util.MD5Utils;
import com.jd.basic.util.StrUtils;
import com.jd.org.domain.Employee;
import com.jd.org.mapper.EmployeeMapper;
import com.jd.org.service.IEmployeeService;
import com.jd.user.domain.Logininfo;
import com.jd.user.mapper.LogininfoMapper;
import com.jd.user.mapper.UserMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 业务实现类：员工
 */
@Service
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {
    @Autowired
    private LogininfoMapper logininfoMapper;
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public void save(Employee employee) {
        //添加logininfo对象
        //获取随机盐值
        String salt = StrUtils.getComplexRandomString(32);
        //获取加密后的密码
        String md5Password = MD5Utils.encrypByMd5(employee.getPassword() + salt);
        employee.setSalt(salt);
        employee.setPassword(md5Password);
        Logininfo logininfo = employee2Logininfo(employee);
        logininfoMapper.save(logininfo); //自增id

        //添加employee
        employee.setLogininfo_id(logininfo.getId());
        super.save(employee);
        //如果添加时选了角色
        //保存中间表的信息
        if (employee.getRole_id()!=null) {
            employeeMapper.saveEmployeeRole(employee.getId(),employee.getRole_id());
        }

    }

    private Logininfo employee2Logininfo(Employee employee) {
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee, logininfo);
        //当前对象为管理员
        logininfo.setType(0);
        return logininfo;
    }

    @Override
    public void remove(Long id) {
        Employee employee = employeeMapper.loadById(id);
        Long logininfo_id = employee.getLogininfo_id();
        //删除登录信息表数据
        logininfoMapper.remove(logininfo_id);
        //删除t_employee:删除中间表信息：t_employee_role
        employeeMapper.removeEmployeeRoleByEmployeeId(id);
        //删除employee表数据
        super.remove(id);
    }
    @Override
    public void update(Employee employee) {
        //1.处理密码和盐值
        //获取随机盐值
        String salt = StrUtils.getComplexRandomString(32);
        //获取加密之后的密码
        String md5Pwd = MD5Utils.encrypByMd5(employee.getPassword()+salt);
        employee.setSalt(salt);
        employee.setPassword(md5Pwd);

        Logininfo logininfo = employee2Logininfo(employee);
        Long logininfo_id = employeeMapper.loadById(employee.getId()).getLogininfo_id();
        logininfo.setId(logininfo_id);

        logininfoMapper.update(logininfo);
        //登录信息id和店铺信息id不能修改

        //删除中间表数据
        employeeMapper.removeEmployeeRoleByEmployeeId(employee.getId());
        //选了角色
        if (employee.getRole_id()!=null) {
            employeeMapper.saveEmployeeRole(employee.getId(),employee.getRole_id());
        }

        super.update(employee);

    }

    @Override
    public void patchDelete(Long[] ids) {
        for (Long id : ids) {
            remove(id);
        }
        super.patchDelete(ids);
    }


    @Override
    public List<Employee> list(Long shopId) {
        return employeeMapper.list(shopId);
    }
}
